import Module from "./Module"
import ViewmodeData from "../data/ViewmodeData"

import CommonControls from "../controls/CommonControls"

export default class CommonControlsModule extends Module {
  modeControls: Record<number, { mode: number; controls: CommonControls }>
  viewmodeData: ViewmodeData
  constructor() {
    super(...arguments)
    this.name = "common-controls"
  }
  async init(e, t) {
    this.modeControls = {}
    t.market.waitForData(ViewmodeData).then(e => (this.viewmodeData = e))
  }
  startRotateTransition(e, t, i = !0) {
    return this.checkControlsForAction(n => n.startRotateTransition(e, t, i))
  }
  startZoomTransition(e, t, i = !0) {
    return this.checkControlsForAction(n => n.startZoomTransition(e, t, i))
  }
  startTranslateTransition(e, t, i = !0) {
    return this.checkControlsForAction(n => n.startTranslateTransition(e, t, i))
  }
  stop() {
    return this.checkControlsForAction(e => (e.stop(), Promise.resolve()))
  }
  checkControlsForAction(e) {
    if (this.viewmodeData && null !== this.viewmodeData.currentMode) {
      if (this.modeControls[this.viewmodeData.currentMode]) {
        return e(this.modeControls[this.viewmodeData.currentMode].controls)
      }
    }
    return Promise.reject("checkControlsForAction() -> Current view mode is null")
  }
  addControls<T extends CommonControls>(e: number, t: T, i = !1) {
    ;(this.modeControls[e] && !i) || (this.modeControls[e] = { mode: e, controls: t })
  }
}
